﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace z.Controllers {
	[z.Filters.InitializeSimpleMembership]
	public class InstallController : Controller {
		public ActionResult Index() {
			return View();
		}
		[Authorize(Roles = "Admin")]
		public ActionResult SamData() {
			using (m.db.Database db = new m.db.Database()) {
				if (db.Products.Count() > 0) {
					return RedirectToAction("SamDataFalse");
				} else {
					//System.IO.StreamReader sr = new StreamReader(Server.MapPath("~/Controllers/install.sql"));
					//db.Database.ExecuteSqlCommand(sr.ReadToEnd());
					string cmd = string.Format("USE [master]\nALTER DATABASE [Sem3] SET SINGLE_USER WITH ROLLBACK IMMEDIATE\n"+
						"RESTORE DATABASE [Sem3] FROM  DISK = N'{0}' WITH  FILE = 3, Move 'Sem3' to '{1}',Move 'Sem3_log' to '{2}',  KEEP_REPLICATION,  NOUNLOAD,  REPLACE,  STATS = 5" +
						"\nALTER DATABASE [Sem3] SET MULTI_USER",
						Server.MapPath("~/Content/Sem3.bak"), Server.MapPath("~/App_Data/Sem3.mdf"), Server.MapPath("~/App_Data/Sem3.log"));
					db.Database.ExecuteSqlCommand(cmd);
					db.SaveChanges();
					return View();
				}
			}
		}
		public ActionResult SamDataFalse() {
			return View();
		}
	}
}
